

        package com.example.myapplication;

        import android.content.ContentValues;
        import android.content.Context;
        import android.database.Cursor;
        import android.database.SQLException;
        import android.database.sqlite.SQLiteDatabase;
        import android.database.sqlite.SQLiteException;
        import android.database.sqlite.SQLiteOpenHelper;
        import android.util.Log;
        import android.widget.Toast;

        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.OutputStream;
        import java.util.ArrayList;

public class DataBaseHelper extends SQLiteOpenHelper {
    private static final String TABLE = "wars";
    private static final String ID = "id";
    private static final String LEVEL = "level";
    private static final String STATUS = "status";
    private static final String NAME = "name";
    private static final String LAYOUT = "layout";
    private static final String WALKTHROUGH = " walkthrough";
    private static DataBaseHelper singleInstance = null;

    // The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/com.example.myapplication/databases/";
    private static String DB_NAME = "aklotski.s3db";
    private SQLiteDatabase myDataBase;

    private final Context myContext;

    public static DataBaseHelper getInstance(Context context)  {
        if(singleInstance!=null)
        {

        }
        else {
            try{
                singleInstance=new DataBaseHelper(context.getApplicationContext());
                singleInstance.createDataBase();
                singleInstance.openDataBase();
            }catch (IOException e){
                Toast.makeText(context,"Error occurred while getINstance",Toast.LENGTH_SHORT).show();
            }


        }
        return singleInstance;
    }

    DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);//创建数据库
        this.myContext = context;
    }

    private boolean dbExit;
    public void createDataBase() throws IOException {
        dbExit=checkDataBase();
        if (dbExit)
        {

        }
        else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            }catch (IOException e  )
            {
                Log.d("msg","error when copyDataBase");
            }
        }

    }



    private boolean checkDataBase(){
        SQLiteDatabase checkDb=null;
        try {
            getReadableDatabase();
            String  FileName = DB_PATH + DB_NAME;
            checkDb=SQLiteDatabase.openDatabase(FileName,null,SQLiteDatabase.OPEN_READONLY);
        }catch(SQLException e){

        }
        if(checkDb!=null)
        {
            checkDb.close();
            return false;
        }
        else
        {

            return true;
        }
    }
    /**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     * */
    public void copyDataBase() throws IOException {
        InputStream myInput = myContext.getAssets().open("aklotski.s3db");
        OutputStream myOutput = new FileOutputStream( DB_PATH + DB_NAME);

        byte[] bytes = new byte[1024];
        int len;
        while ((len = myInput.read(bytes)) > 0) {
            myOutput.write(bytes, 0, len);
        }

        myOutput.flush();//强制缓冲区数据立即输出
        myOutput.close();
        myInput.close();
    }

    public void openDataBase() {


        myDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);

    }


    @Override
    public void onCreate(SQLiteDatabase db)
    {


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }




    public WarRecord getWar(int warId) {

        Cursor cursor = myDataBase.rawQuery(
                "select " + ID + ", " + LEVEL + ", " + STATUS + ", " + NAME + ", " + LAYOUT + "," + WALKTHROUGH + " from " + TABLE + " where " + ID + "=" +warId, null);
        if(cursor==null) {

        }
        cursor.moveToFirst();
        return new WarRecord(cursor.getInt(0), cursor.getInt(1),
                cursor.getInt(2), cursor.getString(3),
                cursor.getString(4), cursor.getString(5));

    }

    public WarRecord getNextWar(int warId) {
        int level = 0;
        Cursor cursor = myDataBase.rawQuery("select " + LEVEL + " from " + TABLE + " where " + ID + "=" +warId, null);
        if(cursor != null) {
            cursor.moveToFirst();
            level = cursor.getInt(0);
        }

        cursor = myDataBase.rawQuery(
                "select " + ID + ", " + LEVEL + ", " + STATUS + ", " + NAME + ", " + LAYOUT + "," + WALKTHROUGH + " from " + TABLE + " where " + STATUS + "=0 and " + ID + ">" +warId+  " and "+ LEVEL+">=" + level  , null);
        if(cursor == null) {
            cursor = myDataBase.rawQuery(
                    "select " + ID + ", " + LEVEL + ", " + STATUS + ", " + NAME + ", " + LAYOUT + "," + WALKTHROUGH + " from " + TABLE + " where " + ID + "=13 limit 1", null);
        }
        cursor.moveToFirst();//指向查询结果的第一个位置
        return new WarRecord(cursor.getInt(0), cursor.getInt(1),
                cursor.getInt(2), cursor.getString(3),
                cursor.getString(4), cursor.getString(5));

    }

    public ArrayList<WarRecord> getWars(int level) {
        ArrayList<WarRecord> wars = new ArrayList<WarRecord>();
        try {
            Cursor cursor = myDataBase.rawQuery(
                    "select " + ID + ", " + LEVEL + ", " + STATUS + ", " + NAME + ", " + LAYOUT + "," + WALKTHROUGH + " from " + TABLE + " where " + LEVEL + "="
                            + level, null);
            if (cursor.moveToFirst()) {
                do {
                    wars.add(new WarRecord(cursor.getInt(0), cursor.getInt(1),
                            cursor.getInt(2), cursor.getString(3),
                            cursor.getString(4), cursor.getString(5)));
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            Log.e("DataBaserHelper",
                    "Error, select ... from " + TABLE + ":" + e.toString());
        }
        return wars;
    }

    public boolean getBMusic() {
        try {
            Cursor cursor = myDataBase.rawQuery("select bmusic from system", null);
            if (cursor.moveToFirst()&&cursor.getInt(0) == 1) {
                    return true;
                }
            }catch (Exception e) {
            Log.e("DataBaserHelper",
                    "Error, select bmusic from system:" + e.toString());
        }
        return false;
    }

    public void setBMusic (boolean bMusic) {

        try {
            ContentValues cv = new ContentValues();
            if(bMusic) {
                cv.put("bmusic", 1);
            } else {
                cv.put("bmusic", 0);
            }
            myDataBase.update("system", cv, null, null);
        } catch (Exception e) {
            Toast.makeText(myContext,"Error, update system set bmusic:",Toast.LENGTH_SHORT).show();
        }
    }

    public void updateWarStatus(int wid, int status) {
        try {


            ContentValues args = new ContentValues();
            args.put(ID,wid);
            args.put(STATUS, status);
            myDataBase.update(TABLE, args, ID + "=?" , new String[]{String.valueOf(wid)});


        } catch (Exception e) {
           Toast.makeText(myContext,"Error while updating",Toast.LENGTH_SHORT).show();
        }
    }
}